package com.joysuch.wwyt.core.quartz;

import com.joysuch.wwyt.core.bean.BaseUserBean;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.entity.BaseImageRepository;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.core.service.BaseImageRepositoryService;
import com.joysuch.wwyt.core.service.BaseUserFaceLogService;
import com.joysuch.wwyt.core.service.BaseUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xsx
 * @version 1.0
 * @date 2023/3/15 10:33
 */
@Component
@Slf4j
public class BaseUserFaceLogJob {

    @Autowired
    private BaseUserFaceLogService baseUserFaceLogService;

    @Autowired
    private BaseUserService baseUserService;

    @Autowired
    private BaseConfigService baseConfigService;

    @Autowired
    private BaseImageRepositoryService baseImageRepositoryService;

    /**
     * 每天3点同步人脸
     * 0 0 3 * * ?
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void updateFaceToken() {
        BaseConfig syncConfig = baseConfigService.findFirstConfig(BaseConfigCodes.BAIDU_FACE_ID_APP_SYNC);
        if (syncConfig==null|| StringUtils.isBlank(syncConfig.getValue())||syncConfig.getValue().equals("关闭")) {
            return;
        }
        BaseConfig appIdConfig = baseConfigService.findFirstConfig(BaseConfigCodes.BAIDU_FACE_ID_APP_ID);
        if (appIdConfig == null || StringUtils.isBlank(appIdConfig.getValue())) {
            return;
        }
        BaseConfig appKeyConfig = baseConfigService.findFirstConfig(BaseConfigCodes.BAIDU_FACE_ID_APP_KEY);
        if (appKeyConfig == null && StringUtils.isBlank(appKeyConfig.getValue())) {
            return;
        }
        BaseConfig appSecretConfig = baseConfigService.findFirstConfig(BaseConfigCodes.BAIDU_FACE_ID_APP_SECRET);
        if (appSecretConfig == null && StringUtils.isBlank(appSecretConfig.getValue())) {
            return;
        }
        List<BaseUser> userList = baseUserFaceLogService.selectUsers();
        if (CollectionUtils.isNotEmpty(userList)) {
            List<BaseUserBean> baseUserBeans=new ArrayList<>();
            List<Long> imageIds = userList.stream().filter(e->e.getIdCardImgId()!=null).map(BaseUser::getIdCardImgId).collect(Collectors.toList());
            Map<Long, String> idCardMap = baseImageRepositoryService.findByIds(imageIds);
            for (BaseUser baseUser : userList) {
                BaseUserBean bean = new BaseUserBean();
                if (baseUser.getIdCardImgId()!=null) {
                    bean.setIdCardImgId(baseUser.getIdCardImgId());
                    bean.setPath(idCardMap.get(baseUser.getIdCardImgId()));
                }
                bean.setUserId(baseUser.getId());
                baseUserBeans.add(bean);
            }
            baseUserFaceLogService.updateFace(baseUserBeans);
            log.info("同步人脸数量"+userList.size());
        }
    }

}
